//菜单树的配置项
var setting={
    view: {
        selectedMulti: false //是否选择多个节点 默认true
    },
    data: {
        keep: {
            parent:true,   //父节点
            leaf:true      //子节点是否锁定
        },
        simpleData: {
            enable: true,
            idKey : "id", //id
            pIdKey : "pid", //父节点的id :pid的名称 ,默认 Pid
            rootPId : "0" //根节点为空表示最高节点
        }
    },
    callback: {
        beforeRemove: beforeRemove
    }
};

var menuTree={
    //树的初始化
    menuInit:function(){
        pubOptBtn.getHeadBtn("t","#btnList","menuTree",menu);
        pubTree.getTreeObj("/role/queryMenuByRoleId","#treeMenuList");
        $("#addObj").bind("click", {isParent:true}, menuTree.add);
        $("#upObj").bind("click", menuTree.edit);
        $("#delObj").bind("click", menuTree.remove);
    },
    add:function(e){
        var zTree = $.fn.zTree.getZTreeObj("treeMenuList"),
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
        loadUtil = new Load($);
        var menuId="";
        if(undefined != treeNode){
               menuId=treeNode.id;
        }
        loadUtil.load({
            container:'#homeContent',
            url: application.contextPath+"/menu/showAddMenuPage?menuId="+menuId
        });
    },
    edit:function () {
        var zTree = $.fn.zTree.getZTreeObj("treeMenuList"),
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
        loadUtil = new Load($);
        if (nodes.length == 0) {
            layer.msg("请选择一个节点",{icon:2});
            return;
        }
        loadUtil.load({
            container:'#homeContent',
            url: application.contextPath+"/menu/updateMenuPage?menuId="+treeNode.id
        });
    },
    remove:function (e) {
        var zTree = $.fn.zTree.getZTreeObj("treeMenuList"),
            nodes = zTree.getSelectedNodes(),
            treeNode = nodes[0];
        if (nodes.length == 0) {
            layer.msg("请先选择一个节点",{icon:2});
            return;
        }
        zTree.removeNode(treeNode, true);
    }
};

function beforeRemove(treeId, treeNode) {
    var flag=false;
    layer.confirm('确定要删除嘛?', {icon: 7, title:'删除提示',btn:['是','否'],
        yes:function(){
            $.ajax({
                url: application.contextPath + "/menu/delMenuById",
                type: "POST",
                dataType: "json",
                data:{
                    id:treeNode.id,
                    parentId:treeNode.pid
                },
                success: function (res) {
                    if(!res.isSuccess){
                        layer.msg("加载失败"+res.message);
                    }else {
                        loadUtil = new Load($);
                        loadUtil.load({
                            container:'#homeContent',
                            url: application.contextPath+"/menu/showMenuPage?menuId=5"
                        });
                        layer.msg("菜单删除成功",{icon:1});
                    }
                },
                error: function (data) {
                    layer.msg("网络异常，请稍后再试！");
                }
            });
        },
        no:function(){
            flag=false;
            layer.close(layer.index);
        }
    });
    return flag;
}

$(document).ready(function(){
    menuTree.menuInit();
});